package com.example.notepad.Activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.notepad.Adapter.NoteAdapter;
import com.example.notepad.Bean.Content;
import com.example.notepad.R;
import com.getbase.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;
//import java.util.logging.Handler;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class NotepadActivity extends Activity {

    String userName;
    List<Content> list = new ArrayList<Content>();
    private RecyclerView mRecyclerView;
    private NoteAdapter mNoteAdapter;
    private FloatingActionButton noteAdd;
    private FloatingActionButton signOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);

        userName = getIntent().getStringExtra("userName");

        moveStatusbar();
        ininView();
        initData();
        ininOnClick();
    }

    private void moveStatusbar() {
        if(Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    private void ininView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.listview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(NotepadActivity.this));
        mNoteAdapter = new NoteAdapter(list,NotepadActivity.this);
        mRecyclerView.setAdapter(mNoteAdapter);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                mNoteAdapter.setScrollingMenu(null);
            }
        });

        noteAdd = (FloatingActionButton) findViewById(R.id.note_add);
        signOut = (FloatingActionButton) findViewById(R.id.sign_out);
    }

    private void initData() {
        if (list != null){
            list.clear();
        }
        BmobQuery<Content> contentBmobQuery = new BmobQuery<>();
        contentBmobQuery.addWhereEqualTo("belong", userName);
        contentBmobQuery.order("-updatedAt");
        contentBmobQuery.findObjects(new FindListener<Content>() {
            @Override
            public void done(List<Content> object, BmobException e) {
                if (e == null) {
                    mNoteAdapter.setAdd(object);
                } else {
                    Log.e("BMOB", e.toString());
                }
            }
        });
    }

    private void ininOnClick() {
        noteAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(NotepadActivity.this, ContentActivity.class);
                intent.putExtra("userName", userName);
                startActivity(intent);
            }
        });

        signOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(NotepadActivity.this, LoginActivity.class);
                intent.putExtra("flag", "###");
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        list.clear();
        initData();
        mNoteAdapter.notifyDataSetChanged();
    }

}